package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.payconfig.EntityXfTimeZone;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoXfTimeZone;
import com.ocom.payconfig.service.XfTimeZoneService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class XfTimeZoneServiceImpl extends ServiceImpl<IDaoXfTimeZone, EntityXfTimeZone> implements XfTimeZoneService {


	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
	public List<EntityXfTimeZone> getConsZoneList(Long companyId) {
		QueryWrapper<EntityXfTimeZone> queryWrapper = Wrappers.query();
		queryWrapper.eq("com_id", companyId);
		queryWrapper.eq("status", 1);
		queryWrapper.orderByAsc("zone_no");
		return this.list(queryWrapper);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
	public EntityXfTimeZone findByComIdAndNoAndName(Long comId, Integer zoneNo, String zoneName) {
		QueryWrapper<EntityXfTimeZone> queryWrapper = Wrappers.query();
		queryWrapper.eq("com_id", comId);
		if (null != zoneNo){
			queryWrapper.eq("zone_no", zoneNo);
		}
		if (StringUtils.isNotBlank(zoneName)){
			queryWrapper.eq("zone_name", zoneName);
		}
		queryWrapper.last("limit 1");
		return this.getOne(queryWrapper);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId")
	public boolean addXfTimeZone(Long comId, EntityXfTimeZone entityXfTimeZone) {
		return this.save(entityXfTimeZone);
	}


	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId")
	public boolean updateConsZone(Long comId, EntityXfTimeZone entityXfTimeZone) {
		UpdateWrapper<EntityXfTimeZone> updateWrapper = Wrappers.update();
		updateWrapper.eq("com_id", comId);
		updateWrapper.eq("zone_no", entityXfTimeZone.getZoneNo());
		updateWrapper.set("zone_name", entityXfTimeZone.getZoneName());
		return this.update(updateWrapper);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
	public EntityXfTimeZone findConsZoneName(Long comId, EntityXfTimeZone entityXfTimeZone) {
		QueryWrapper<EntityXfTimeZone> queryWrapper = Wrappers.query();
		queryWrapper.eq("com_id", comId);
		queryWrapper.eq("zone_name", entityXfTimeZone.getZoneName());
		queryWrapper.ne("zone_no", entityXfTimeZone.getZoneNo());
		queryWrapper.last("limit 1");
		return this.getOne(queryWrapper);
	}
}
